﻿using System.Linq;

namespace dPet.Domain.Models
{
	using Hangerd;
	using Hangerd.Entity;

	public class ServiceItemSKU : EntityBase
	{
		#region Public Properties

		/// <summary>
		/// 服务项
		/// </summary>
		public ServiceItem ServiceItem { get; private set; }

		/// <summary>
		/// SKU属性值ID集合
		/// </summary>
		public string SKUPropertyValueIds { get; private set; }

		/// <summary>
		/// SKU属性值名称集合
		/// </summary>
		public string SKUPropertyValueNames { get; private set; }

		/// <summary>
		/// 价格
		/// </summary>
		[RecordModify]
		public decimal UnitPrice { get; private set; }

		#endregion

		#region Constructors

		private ServiceItemSKU() { }

		public ServiceItemSKU(ServiceItem serviceItem, ServiceSKUPropertyValue[] propertyValues)
		{
			this.ServiceItem = serviceItem;

			this.SetOptionalSKUPropertyValues(propertyValues);
		}

		#endregion

		#region Public Methods

		public void SetUnitPrice(decimal unitPrice)
		{
			if (unitPrice < 0)
			{
				throw new HangerdException("服务项价格不可小于0");
			}

			this.UnitPrice = unitPrice;
		}

		#endregion

		#region Private Methods

		private void SetOptionalSKUPropertyValues(ServiceSKUPropertyValue[] propertyValues)
		{
			if (propertyValues == null)
			{
				this.SKUPropertyValueIds = string.Empty;
				this.SKUPropertyValueNames = "-";

				return;
			}

			this.SKUPropertyValueIds = string.Join("|", propertyValues.Select(pv => pv.Id).ToArray());
			this.SKUPropertyValueNames = string.Join("、", propertyValues.Select(pv => pv.Value).ToArray());
		}

		#endregion
	}
}
